package com.yangtu.nearbyshop.merc.web;

import com.yangtu.nearbyshop.merc.annotation.LoginMerc;
import com.yangtu.nearbyshop.merc.service.MercTokenManager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.yangtu.nearbyshop.merc.service.MercOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/merc/order")
@Validated
public class MercOrderController {
    private final Log logger = LogFactory.getLog(MercOrderController.class);

    @Autowired
    private MercOrderService mercOrderService;

    /**
     * 订单列表
     *
     * @param mercId   用户ID
     * @param showType 订单信息
     * @param page     分页页数
     * @param size     分页大小
     * @return 订单列表
     */
    @GetMapping("list")
    public Object list(@LoginMerc Integer mercId,
                       @RequestParam(defaultValue = "0") Integer showType,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer size) {
        return mercOrderService.list(MercTokenManager.getMercNo(mercId), showType, page, size);
    }

    /**
     * 订单详情
     *
     * @param mercId  用户ID
     * @param orderId 订单ID
     * @return 订单详情
     */
    @GetMapping("detail")
    public Object detail(@LoginMerc Integer mercId, @NotNull Integer orderId) {
        return mercOrderService.detail(mercId, orderId);
    }

    /**
     * 订单申请退款
     *
     * @param mercId 用户ID
     * @param body   订单信息，{ orderId：xxx }
     * @return 订单退款操作结果
     */
    @PostMapping("refund")
    public Object refund(@LoginMerc Integer mercId, @RequestBody String body) {
        return mercOrderService.refund(mercId, body);
    }

    /**
     * 确认收货
     *
     * @param mercId 用户ID
     * @param body   订单信息，{ orderId：xxx }
     * @return 订单操作结果
     */
    @PostMapping("confirm")
    public Object confirm(@LoginMerc Integer mercId, @RequestBody String body) {
        return mercOrderService.confirm(mercId, body);
    }

    /**
     * 商户收益明细
     *
     * @param mercId 用户ID
     * @param startDt   起始时间
     * @param endDt   结束时间
     * @param page   页数
     * @param size   每页条数
     * @return 订单操作结果
     */
    @GetMapping("mercProfit")
    public Object mercProfit(@LoginMerc Integer mercId,
                             String startDt,
                             String endDt,
                             @RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer size) {
        return mercOrderService.profitList(MercTokenManager.getMercNo(mercId), startDt, endDt, page, size);
    }

    /**
     * 商户推荐收益明细
     *
     * @param mercId 用户ID
     * @param startDt   起始时间
     * @param endDt   结束时间
     * @return 订单操作结果
     */
    @GetMapping("mercReferProfit")
    public Object mercReferProfit(@LoginMerc Integer mercId,
                             String startDt,
                             String endDt) {
        return mercOrderService.referProfitList(MercTokenManager.getMercNo(mercId), startDt, endDt);
    }
}